/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcpclient;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.ByteStream;

/**
 *
 * @author Hnmtuan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String sentences = "";
        Integer modifiedSentences = 0;
        String hostNames = "192.168.30.48";
        Integer portNums = 1610;

        BufferedReader inFromUser = null;
        Socket clientSocket = null;
        DataOutputStream output = null; // Send data to server
        DataInputStream input = null; // Get data from server
        FileOutputStream fos = null;
        InputStream in = null;
        try {

            // Create Client Socket
            clientSocket = new Socket(hostNames, portNums);

            // Create InputStream to read data from server
            input = new DataInputStream(clientSocket.getInputStream());
            in = clientSocket.getInputStream();

            System.out.println("Waiting received data from server .... !\n");

            //  Get data from server
            int nof_files = ByteStream.toInt(in);
            System.out.println("Nums : " + nof_files);

            String file_name = ByteStream.toString(in);
            System.out.println("File name : " + file_name);

            File file=new File(file_name);
            ByteStream.toFile(in, file);
            System.out.println("Received file successfully ... !");
            
        } catch (UnknownHostException ex) {
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            //Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            if(clientSocket==null){System.out.println("CREATE CLIENT SOCKET FAILED!");return;}
            try {
                clientSocket.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
}
